<template>
    <addLeaveStudent ref="addLeaveStudent" />
    <addFileView ref="addFileView" />
    <div class="app-table">
        <div class="app-body">
            <div class="app-search">
                <el-form :inline="true" class="demo-form-inline" v-model="form">
                    <el-form-item label="姓名" style="width:200px">
                        <el-input placeholder="请输入姓名" v-model="form.studentName"></el-input>
                    </el-form-item>
                    <el-form-item label="学号" style="width:200px">
                        <el-input placeholder="请输入学号" v-model="form.studentNumber"></el-input>
                    </el-form-item>

                    <el-form-item>
                        <el-button class="serach_btn" @click="onSubmit" @keyup.enter="searchEnter">搜索</el-button>
                        <el-button class="add_btn" @click="batchUpdate">批量导入</el-button>
                        <el-button class="export_btn" @click="download">下载</el-button>
                        <el-button class="export_btn" @click="addLeaveStudent">添加走读生</el-button>
                    </el-form-item>
                </el-form>
            </div>
            <el-table size="large" :data="list">
                <el-table-column prop="id" label="序号" width="100" />
                <el-table-column prop="studentName" label="姓名" />
                <el-table-column prop="studentNumber" label="学号" />
                <el-table-column prop="campus" label="校区">
                    <template #default="scope">
                        <span v-if="scope.row.campus == 1">校本部</span>
                        <span v-if="scope.row.campus == 2">东校区</span>
                    </template>
                </el-table-column>
                <el-table-column prop="teacherName" label="操作人" />
                <el-table-column prop="createTime" label="操作时间" min-width="140" />
                <el-table-column label="操作">
                    <template #default="scope">
                        <el-button size="small" type="danger" @click="handleDelete(scope.row.id)">
                            取消走读
                        </el-button>
                    </template>
                </el-table-column>
                <template v-slot:empty>
                    <el-empty description="暂无数据"></el-empty>
                </template>
            </el-table>
            <div style="margin-top: 10px">
                <el-config-provider :locale="locale">
                    <el-pagination :total="total" :page-size="pageSize" :current-page="page"
                        layout="prev, pager, next, jumper" @size-change="changePageSize"
                        @current-change="changeCurrentPage">
                    </el-pagination>
                </el-config-provider>
            </div>
        </div>
        <bottomView></bottomView>
    </div>
</template>
<script>
import addLeaveStudent from "../../components/studentLeaveLog/addLeaveStudent";
import addFileView from "@/components/studentLeaveLog/addExcelFile.vue";
import { ElConfigProvider, ElMessageBox, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { formatTime1 } from "@/utils/dateFormat";
import bottomView from "@/components/bottom.vue";

export default {
    name: "classTeacherList",
    data() {
        return {
            type: 0,
            title: "",
            page: 1,
            pages: 0,
            pageSize: 10,
            total: 0,
            limit: 10,
            list: [],
            classList: [],
            departmentList: [],
            form: {
                studentName: "",
                studentNumber: ""
            },
        };
    },
    components: {
        ElConfigProvider,
        ElMessageBox,
        bottomView,
        ElMessage,
        addLeaveStudent,
        addFileView,
    },
    setup() {
        return {
            locale: zhCn,
        };
    },
    mounted() {
        this.getStudentList();
    },
    methods: {

        searchEnter: function (e) {
            // 使用 which 和 keyCode 属性来解决兼容问题
            var keyCode = window.event ? e.keyCode : e.which;
            var val = e.target.value;
            if (keyCode == 13 && val) {
                this.page = 1;
                this.getStudentList();
            }
        },

        handleDelete(id) {
            ElMessageBox.confirm("确定要取消该学生走读吗？", "提醒", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    httpService("/javaApi/baseDayStudent/deleteById", "id=" + id, "post").then(
                        (data) => {
                            if (data.code == 200) {
                                ElMessage.success("取消成功");
                                this.getStudentList();
                            } else {
                                ElMessage.error("取消失败");
                            }
                        }
                    );
                })
                .catch(() => {
                });
        },


        onSubmit() {
            this.page = 1;
            this.getStudentList();
        },
        // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
        changePageSize(size) {
            this.pageSize = size;
            this.limit = size;
            this.getStudentList();
        },

        // 改变页码，page：回调参数，表示当前选中的“页码”
        changeCurrentPage(page) {
            this.page = page;
            this.getStudentList();
        },
        getStudentList() {
            httpService(
                "/javaApi/baseDayStudent/list/" + this.page + '/' + this.pageSize,
                {
                    studentName: this.form.studentName,
                    studentNumber: this.form.studentNumber,
                },
                "get"
            ).then((data) => {
                this.total = data.total
                if (data.list) {
                    data.list.map(item => {
                        return item.createTime = formatTime1(item.createTime)
                    })
                    this.list = data.list
                }
            });
        },

        addLeaveStudent() {
            this.$refs.addLeaveStudent.dialogFormVisible = true
        },
        download() {
            httpService(
                "/javaApi/baseDayStudent/exportExcel", {}, "get"
            ).then((data) => {
                // console.log(data);
                window.open("https://wx.zhengzhong.cn" + data.message, '_blank')
            });
        },

        batchUpdate() {
            this.$refs.addFileView.dialogFormVisible = true
            this.$refs.addFileView.title = "批量导入";
            this.$refs.addFileView.uploadApiUrl = "/javaApi/baseDayStudent/importExcel";
        },
    },
};
</script>
<style scoped>
.app-table {
    background: #eff3f5;
    padding: 10px;
    width: calc(100vw - 220px);
}

/* .app-search {} */


.app-body {
    background: white;
    padding: 20px 20px 10px 20px;
    margin-bottom: 10px;
    min-height: calc(100vh - 204px);
}

.serach_btn {
    background-color: #007EFF;
    color: #fff;
    border-radius: 8px;
    border: none;
}

.add_btn {
    background-color: #00c78c;
    color: #fff;
    border-radius: 8px;
    border: none;
}

.export_btn {
    background-color: #FF7200;
    border-radius: 8px;
    color: #fff;
    border: none;
}

.edit_btn {
    background-color: #fff;
    color: #007EFF;
    border: none;
}

.del_btn {
    background-color: #fff;
    color: #FF0000;
    border: none;
}
</style>
